package com.example.zhyq.park

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Adapter
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import androidx.databinding.adapters.AdapterViewBindingAdapter.OnItemSelected
import androidx.recyclerview.widget.ListAdapter
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.ParkBeanItem
import com.example.base.mvvm.BaseActivity
import com.example.zhyq.R
import com.example.zhyq.databinding.ActivityParkEditBinding
import dagger.hilt.android.AndroidEntryPoint

/**
 *  修改车位
 */
@Route(path = "/app/ParkEditActivity")
@AndroidEntryPoint
class ParkEditActivity : BaseActivity<ActivityParkEditBinding,ParkViewModel>() {

    @Autowired
    lateinit var bean:ParkBeanItem

    var area = ""
    var type = ""
    var status = ""
    override fun initView() {
        ARouter.getInstance().inject(this)

        binding.headPark.headerTitle.text = "车位编辑"
        binding.headPark.headerBack.setOnClickListener {
            finish()
        }

        binding.headPark.headerAdd.apply {
            setImageResource(R.drawable.baseline_check_24)
            setOnClickListener {
                val beanItem = ParkBeanItem()
                beanItem.areaId = bean.id
                beanItem.areaName = bean.areaName
                beanItem.id = bean.id
                beanItem.licensePlate = binding.etLicense.text.toString()
                beanItem.remarks = binding.etBeizhu.text.toString()
                beanItem.spaceNumber = bean.spaceNumber
                beanItem.spaceStatus = status
                beanItem.spaceType = type
                beanItem.userId = bean.userId
                beanItem.userName = binding.etName.text.toString()

                vm.editPark(bean.id,beanItem)
            }
        }

        binding.etCarNo.setText(bean.spaceNumber)
        binding.etBeizhu.setText(bean.remarks)
        binding.etName.setText(bean.userName)
        binding.etLicense.setText(bean.licensePlate)

        if (bean.spaceStatus=="空闲") {
            binding.drawRg.check(R.id.rb_kx)
        }else if(bean.spaceStatus=="已占用") {
            binding.rbZy.isChecked
        }else {
            binding.drawRg.check(R.id.rb_wh)
        }

        val arr = mutableListOf<String>("A区停车场", "B区停车场", "C区停车场")
        if (!arr.contains(bean.areaName)){
            arr.add(bean.areaName)
        }

        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, arr)
        binding.spArea.adapter = adapter
        binding.spArea.setSelection(arr.indexOf(bean.areaName))

        binding.spArea.onItemSelectedListener=object :OnItemSelectedListener {
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                area=arr[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }


        val arrType = mutableListOf<String>("标准车位","VIP车位")
        if (!arrType.contains(bean.spaceType)){
            arrType.add(bean.spaceType)
        }
        val adapter2 = ArrayAdapter(this,android.R.layout.simple_spinner_item,arrType)
        binding.spType.adapter = adapter2
        binding.spType.setSelection(arrType.indexOf(bean.spaceType))

        binding.spType.onItemSelectedListener=object :OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                type=arrType[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }

        binding.drawRg.setOnCheckedChangeListener { radioGroup, i ->
            if (i==R.id.rb_kx){
                status="空闲"
            }else if (i==R.id.rb_zy){
                status="已占用"
            }else{
                status="维护中"
            }
        }
    }


    override fun initData() {
        vm.editParkSuc.observe(this) {
            ToastUtils.showLong("修改成功")
            finish()
        }
    }
}